package cn.maydaymemory.maydaycore.configuration;

import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.*;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class LanguageManager{
    private Map<Locale,YamlConfiguration> languages;
    private YamlConfiguration defaultLang;
    private File root;
    private JavaPlugin plugin;
    public LanguageManager(JavaPlugin plugin){
        this.plugin=plugin;
        languages=new HashMap<>();
        root=new File(plugin.getDataFolder(),"lang");
    }
    public void preload(String languageTag){
        getLang(languageTag);
    }
    public void preload(Locale locale){
        getLang(locale);
    }
    public void preloadDefault(){
        getDefaultLang();
    }
    public YamlConfiguration getLang(){
        return getLang(Locale.getDefault());
    }
    public YamlConfiguration getLang(Locale locale){
        if(!plugin.getDataFolder().exists()) plugin.getDataFolder().mkdir();
        if(!root.exists()) root.mkdir();
        YamlConfiguration lang = languages.get(locale);
        if(lang==null){
            File langPath=new File(root,"lang_"+locale.toString()+".yml");
            if (!langPath.exists()) {
                InputStream input = plugin.getResource("lang_"+locale.toString()+".yml");
                if(input!=null){
                    try {
                        if(langPath.createNewFile()) {
                            OutputStream output = new FileOutputStream(langPath);
                            byte[] b = new byte[1024];
                            int length;
                            while ((length = input.read(b)) != -1) {
                                output.write(b, 0, length);
                            }
                            lang=YamlConfiguration.loadConfiguration(langPath);
                            output.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                else {
                    lang=getDefaultLang();
                }
            }
            else lang = YamlConfiguration.loadConfiguration(langPath);
            languages.put(locale,lang);
        }
        return lang;
    }
    public YamlConfiguration getLang(String languageTag){
        return getLang(Locale.forLanguageTag(languageTag));
    }
    public YamlConfiguration getDefaultLang(){
        if(!plugin.getDataFolder().exists()) plugin.getDataFolder().mkdir();
        if(!root.exists()) root.mkdir();
        if(defaultLang == null){
            File langPath=new File(root,"lang.yml");
            if (!langPath.exists()) {
                InputStream input = plugin.getResource("lang.yml");
                if(input!=null){
                    try {
                        if(langPath.createNewFile()) {
                            OutputStream output = new FileOutputStream(langPath);
                            int length;
                            byte[] b = new byte[4096];
                            while ((length = input.read(b)) != -1) {
                                output.write(b, 0, length);
                            }
                            output.close();
                            defaultLang=YamlConfiguration.loadConfiguration(langPath);
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }else{
                    return null;
                }
            }
            else{
                defaultLang = YamlConfiguration.loadConfiguration(langPath);
            }
        }
        return defaultLang;
    }
}
